package org.jrpc.core.factory;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;

/**
 * Created by chenyuncong on 2019/4/22.
 */

public class JrpcConfiguration implements EnvironmentAware {

    @Value("jrpc.address")
    private String address;

    @Value("jrpc.scannerPath")
    private String scannerPath;

    @Value("jrpc.serviceName")
    private String serviceName;

    @Value("jrpc.host")
    private String host;

    @Value("jrpc.port")
    private String port;

    @Value("jrpc.status")
    private String status="UP";

    @Value("jrpc.version")
    private String version="all";

    @Value("jrpc.group")
    private String group="all";

    @Override
    public void setEnvironment(Environment environment) {
        Value v = null;
        Object o = null;
        for(Field field :this.getClass().getDeclaredFields() ){
            v = field.getAnnotation(Value.class);
            o = environment.getProperty(v.value());
            if(o!=null){
                field.setAccessible(true);
                try {
                    field.set(this,o);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }

        }
    }

    public String getServiceName() {
        return serviceName;
    }

    public void setServiceName(String serviceName) {
        this.serviceName = serviceName;
    }

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public String getPort() {
        return port;
    }

    public void setPort(String port) {
        this.port = port;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getScannerPath() {
        return scannerPath;
    }

    public void setScannerPath(String scannerPath) {
        this.scannerPath = scannerPath;
    }


}
